require 'pp'
require 'erb'
class Show < ActiveRecord::Base
  has_one :feed
  has_many :episodes
  def create_directory
    configs = Configuration.find(:first)
    pp configs
    base_dir = configs.video_files_root
    directory_name = self.title
    puts base_dir
    directory_name = directory_name.gsub(/ /, "-")
    puts directory_name
    full_path = base_dir  + "/" + directory_name
    FileUtils::mkdir full_path
    self.directory = full_path
    self.save
  end
  def try_to_associate_tvrss_feed 
    #try and associate a tvrss feed to this show
    #title_search_string = self.title.gsub(/ /,"+")
    title_search_string = ERB::Util::u(self.title)
    puts title_search_string
    tvrss_url = "http://tvrss.net/search/index.php?show_name=#{title_search_string}&show_name_exact=true&mode=rss"
    puts tvrss_url
    
    #crete a new feed
    feed = Feed.new({:url => tvrss_url})
    feed.fetch
    if !feed.rss.items.first.nil?
      feed.show_id = self.id
      feed.save
      return true
    end
    return false
  end

  def destroy
    begin
      FileUtils::rmdir self.directory
    rescue
    end
    super
  end
end
